package com.csun.cmny.provider.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.csun.cmny.base.exception.BusinessException;
import com.csun.cmny.config.properties.CmnyProperties;
import com.csun.cmny.core.utils.MD5Util;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.hualong.HuaLongDoctorAuditDto;
import com.csun.cmny.provider.model.enums.UacEmpStatusEnum;
import com.csun.cmny.provider.model.enums.UacUserSourceEnum;
import com.csun.cmny.provider.model.enums.UacUserTypeEnum;
import com.csun.cmny.provider.model.vo.hualong.*;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.utils.Md5Util;
import com.csun.cmny.util.PublicUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-10-23 16:27
 * @Description:
 */
@Service
public class HuaLongServiceImpl implements HuaLongService {

    @Resource
    private CmnyProperties cmnyProperties;
    @Resource
    private UacHuaLongUserService uacHuaLongUserService;
    @Resource
    private UacUserService uacUserService;
    @Resource
    private UacGroupService uacGroupService;
    @Resource
    private UacGroupUserService uacGroupUserService;
    @Resource
    private UacRoleService uacRoleService;
    @Resource
    private UacRoleUserService uacRoleUserService;

//    String host = "https://testapi.e-cares.cn/";
//    String host = cmnyProperties.getHualong().getHost();
//    static final String key = "test";
//    String key = cmnyProperties.getHualong().getKey();
//    static final String secret = "testSecret";
//    String secret = cmnyProperties.getHualong().getSecret();

    /**
     * 登录
     */
    static final String LOGIN_URL = "%sshj/v1/staff/login?key=%s&secret=%s";
    /**
     * 医生列表
     */
    static final String DOCTOR_LIST_URL = "%sshj/v1/doctor/lists?key=%s&secret=%s&page=%s&size=%s";
    /**
     * 医生详情
     */
    static final String DOCTOR_INFO_URL = "%sshj/v1/doctor/info?key=%s&secret=%s&id=%s";
    /**
     * 主管列表
     */
    static final String SUPER_LIST_URL = "%sshj/v1/super/lists?key=%s&secret=%s&page=%s&size=%s";
    /**
     * 主管详情
     */
    static final String SUPER_INFO_URL = "%sshj/v1/super/info?key=%s&secret=%s&id=%s";
    /**
     * 护工列表
     */
    static final String NURSE_LIST_URL = "%sshj/v1/nurse/lists?key=%s&secret=%s&page=%s&size=%s";
    /**
     * 护工详情
     */
    static final String NURSE_INFO_URL = "%sshj/v1/nurse/info?key=%s&secret=%s&id=%s";
    /**
     * 护工获取对应楼栋
     */
    static final String NURSE_BUILD_URL = "%sshj/v1/nurse/build/info?key=%s&secret=%s&id=%s";
    /**
     * 班组列表
     */
    static final String TEAM_LIST_URL = "%sshj/v1/staff/team/lists?key=%s&secret=%s&page=%s&size=%s";
    /**
     * 班组详情
     */
    static final String TEAM_INFO_URL = "%sshj/v1/staff/team/detail?key=%s&secret=%s&id=%s";
    /**
     * 长者列表
     */
    static final String ELDER_LIST_URL = "%sshj/v1/staff/elder/lists?key=%s&secret=%s&page=%s&size=%s";
    /**
     * 长者详情
     */
    static final String ELDER_INFO_URL = "%sshj/v1/staff/elder/detail?key=%s&secret=%s&id=%s";
    /**
     * 获取楼栋房间
     */
    static final String BUILD_LIST_URL = "%sshj/v1/build/lists?key=%s&secret=%s";
    /**
     * 获取床位老人
     */
    static final String ROOM_BED_URL = "%sshj/v1/build/room_bed_info?key=%s&secret=%s";
    /**
     * 医生查看预约详情
     */
    static final String APPOINTMENT_INFO_URL = "%sshj/v1/elder/doctor/appointment_info?key=%s&secret=%s&id=%s";
    /**
     * 医生可查看预约列表
     */
    static final String APPOINTMENT_LIST_URL = "%sshj/v1/elder/doctor/appointment_lists?key=%s&secret=%s&page=%s&size=%s";
    /**
     * 医生审核预约信息
     */
    static final String DOCTOR_AUDIT_URL = "%sshj/v1/elder/doctor/audit?key=%s&secret=%s";
    /**
     * 医生端（修改密码）
     */
    static final String DOCTOR_MODIFY_PWD_URL = "%sshj/v1/doctor/edit/password?key=%s&secret=%s";
    /**
     * 护士端（修改密码）
     */
    static final String NURSE_MODIFY_PWD_URL = "%sshj/v1/nurse/edit/password?key=%s&secret=%s";

    @Autowired
    private RestTemplate myRestTemplate;

    /**
     * 账号密码登录
     *
     * @param account   手机号
     * @param password  密码
     * @param role      用户角色，默认为1（1：医生 2：护工 3：医护主管）
     *
     * @return  the uac user
     */
    @Override
    public UacUser login(String account, String password, String role) {

        String loginUrl = String.format(LOGIN_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret());

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        MultiValueMap map = new LinkedMultiValueMap();
        map.add("account", account);
        map.add("password", MD5Util.getMD5String(password));
        map.add("role", role);

        HttpEntity request = new HttpEntity<>(map, headers); //组装

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(loginUrl, HttpMethod.POST, request, HuaLongVo.class);

        UacUser uacUser;
        if (response.getBody().getCode() == 0) {
            // 登录成功
            HuaLongUserVo huaLongUserVo = JSON.parseObject(JSON.toJSONString(response.getBody().getData()), new TypeReference<HuaLongUserVo>() {});

            // 判断用户是已经激活
            UacHuaLongUser huaLongUser = new UacHuaLongUser();
            huaLongUser.setHuaLongId(huaLongUserVo.getId());
            huaLongUser = uacHuaLongUserService.selectOne(huaLongUser);
            if (PublicUtil.isNotEmpty(huaLongUser)) {
//                uacUser = uacUserService.selectByKey(huaLongUser.getId());
                uacUser = uacUserService.findUserInfoByUserId(huaLongUser.getId());
            } else {
                // 新增一条用户记录
                uacUser = new UacUser();
                uacUser.setIdCard(huaLongUserVo.getInfo().getIc());
                uacUser.setUserName(huaLongUserVo.getInfo().getName());
                uacUser.setMobileNo(huaLongUserVo.getInfo().getPhone());
                if (huaLongUserVo.getInfo().getSex().equals(2)) { // 女
                    uacUser.setSex(0);  // 女
                } else {
                    uacUser.setSex(1); // 男
                }
                uacUser.setLoginName(account);
                uacUser.setLoginPwd(Md5Util.encrypt(password));
                uacUser.setType(UacUserTypeEnum.OPERATE.getKey());
                uacUser.setUserSource(UacUserSourceEnum.INSERT.getKey());
                uacUser.setStatus(UacEmpStatusEnum.ENABLE.getType());
                uacUserService.save(uacUser);

                UacGroup uacGroup = new UacGroup();
                uacGroup.setGroupCode("hua_long");
                uacGroup = uacGroupService.selectOne(uacGroup);

                UacGroupUser uacGroupUser = new UacGroupUser();
                uacGroupUser.setUserId(uacUser.getId());
                uacGroupUser.setGroupId(uacGroup.getId());
                uacGroupUserService.save(uacGroupUser);

                uacUser.setGroupId(uacGroup.getId());
                uacGroup.setGroupName(uacGroup.getGroupName());

                huaLongUser = new UacHuaLongUser();
                huaLongUser.setId(uacUser.getId());
                huaLongUser.setAge(huaLongUserVo.getInfo().getAge());
                huaLongUser.setSex(huaLongUserVo.getInfo().getSex());
                huaLongUser.setIc(huaLongUserVo.getInfo().getIc());
                huaLongUser.setName(huaLongUserVo.getInfo().getName());
                huaLongUser.setPhone(huaLongUserVo.getInfo().getPhone());
                huaLongUser.setRole(role);
                huaLongUser.setHuaLongId(huaLongUserVo.getId());
                uacHuaLongUserService.save(huaLongUser);

                // 分配角色
                String roleCode = "";
                switch (role) {
                    case "1" :
                        roleCode = "hua_long_doctor";
                        break;
                    case "2" :
                        roleCode = "hua_long_nurse";
                        break;
                    case "3" :
                        roleCode = "hua_long_super";
                        break;
                }

                // 分配角色
                UacRole uacRole = uacRoleService.findByRoleCode(roleCode);
                uacRoleUserService.saveRoleUser(uacUser.getId(), uacRole.getId());
            }
            

        } else {
            // 登录失败
//            throw new BusinessException(response.getBody().getMsg());
            throw new BadCredentialsException(response.getBody().getMsg());
        }

        return uacUser;
    }

    /**
     * 医生列表
     *
     * @param page 页数 (默认为1)
     * @param size 每页显示条数（默认为10）
     * @return the list hua long doctor vo
     */
    @Override
    public Object getHuaLongDoctorList(Integer page, Integer size) {

        if (PublicUtil.isEmpty(page)) {
            page = 1;
        }
        if (PublicUtil.isEmpty(size)) {
            size = 10;
        }
        String url = String.format(DOCTOR_LIST_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), page, size);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);

        List<HuaLongDoctorVo> huaLongDoctorVoList;
        if (response.getBody().getCode() == 0) {
            // 成功
//            JSONObject object = JSONObject.fromObject(response.getBody().getData());
//            huaLongDoctorVoList = (List<HuaLongDoctorVo>) object.get("lists");

            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }

//        return huaLongDoctorVoList;
    }

    /**
     * 医生详情
     *
     * @param id the id
     * @return the hua long doctor vo
     */
    @Override
    public Object getHuaLongDoctorById(Integer id) {

        String url = String.format(DOCTOR_INFO_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), id);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);
        HuaLongDoctorVo huaLongDoctorVo;
        if (response.getBody().getCode() == 0) {
            // 成功
//            huaLongDoctorVo = JSON.parseObject(JSON.toJSONString(response.getBody().getData()), new TypeReference<HuaLongDoctorVo>() {});
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }
    }

    /**
     * 主管列表
     *
     * @param page 页数 (默认为1)
     * @param size 每页显示条数（默认为10）
     * @return the list hua long super vo
     */
    @Override
    public Object getHuaLongSuperList(Integer page, Integer size) {

        if (PublicUtil.isEmpty(page)) {
            page = 1;
        }
        if (PublicUtil.isEmpty(size)) {
            size = 10;
        }
        String url = String.format(SUPER_LIST_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), page, size);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);

        List<HuaLongSuperVo> huaLongSuperVoList;
        if (response.getBody().getCode() == 0) {
            // 成功
//            JSONObject object = JSONObject.fromObject(response.getBody().getData());
//            huaLongSuperVoList = (List<HuaLongSuperVo>) object.get("lists");
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }

//        return huaLongSuperVoList;
    }

    /**
     * 主管详情
     *
     * @param id the id
     * @return the hua long super vo
     */
    @Override
    public Object getHuaLongSuperById(Integer id) {

        String url = String.format(SUPER_INFO_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), id);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);
        HuaLongSuperVo huaLongSuperVo;
        if (response.getBody().getCode() == 0) {
            // 成功
//            huaLongSuperVo = JSON.parseObject(JSON.toJSONString(response.getBody().getData()), new TypeReference<HuaLongSuperVo>() {});
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }
    }

    /**
     * 护工列表
     *
     * @param page 页数 (默认为1)
     * @param size 每页显示条数（默认为10）
     * @return the list hua long nurse vo
     */
    @Override
    public Object getHuaLongNurseList(Integer page, Integer size) {

        if (PublicUtil.isEmpty(page)) {
            page = 1;
        }
        if (PublicUtil.isEmpty(size)) {
            size = 10;
        }
        String url = String.format(NURSE_LIST_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), page, size);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);

        List<HuaLongNurseVo> huaLongNurseVoList;
        if (response.getBody().getCode() == 0) {
            // 成功
//            JSONObject object = JSONObject.fromObject(response.getBody().getData());
//            huaLongNurseVoList = (List<HuaLongNurseVo>) object.get("lists");

            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }

//        return huaLongNurseVoList;
    }

    /**
     * 护工详情
     *
     * @param id the id
     * @return the hua long nurse vo
     */
    @Override
    public Object getHuaLongNurseById(Integer id) {

        String url = String.format(NURSE_INFO_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), id);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);
        HuaLongNurseVo huaLongNurseVo;
        if (response.getBody().getCode() == 0) {
            // 成功
//            huaLongNurseVo = JSON.parseObject(JSON.toJSONString(response.getBody().getData()), new TypeReference<HuaLongNurseVo>() {});
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }

//        return huaLongNurseVo;
    }

    @Override
    public Object getHuaLongNurseBuildInfoId(Integer id) {

        String url = String.format(NURSE_BUILD_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), id);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);
        if (response.getBody().getCode() == 0) {
            // 成功
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }
    }

    /**
     * 班组列表
     *
     * @param page 页数 (默认为1)
     * @param size 每页显示条数（默认为10）
     * @return the list hua long team vo
     */
    @Override
    public Object getHuaLongTeamList(Integer page, Integer size) {

        if (PublicUtil.isEmpty(page)) {
            page = 1;
        }
        if (PublicUtil.isEmpty(size)) {
            size = 10;
        }
        String url = String.format(TEAM_LIST_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), page, size);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);

        List<HuaLongTeamVo> huaLongTeamVoList;
        if (response.getBody().getCode() == 0) {
            // 成功
//            JSONObject object = JSONObject.fromObject(response.getBody().getData());
//            huaLongTeamVoList = (List<HuaLongTeamVo>) object.get("lists");

            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }

//        return huaLongTeamVoList;
    }

    /**
     * 班组详情
     *
     * @param id the id
     * @return the hua long team vo
     */
    @Override
    public Object getHuaLongTeamById(Integer id) {

        String url = String.format(TEAM_INFO_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), id);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);
        HuaLongTeamVo huaLongTeamVo;
        if (response.getBody().getCode() == 0) {
            // 成功
//            huaLongTeamVo = JSON.parseObject(JSON.toJSONString(response.getBody().getData()), new TypeReference<HuaLongTeamVo>() {});
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }
    }

    /**
     * 长者列表
     *
     * @param page 页数 (默认为1)
     * @param size 每页显示条数（默认为10）
     * @return the list hua long elder vo
     */
    @Override
    public Object getHuaLongElderList(Integer page, Integer size) {

        if (PublicUtil.isEmpty(page)) {
            page = 1;
        }
        if (PublicUtil.isEmpty(size)) {
            size = 10;
        }
        String url = String.format(ELDER_LIST_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), page, size);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);

        List<HuaLongElderVo> huaLongElderVoList;
        if (response.getBody().getCode() == 0) {
            // 成功
//            JSONObject object = JSONObject.fromObject(response.getBody().getData());
//            huaLongElderVoList = (List<HuaLongElderVo>) object.get("lists");
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }

//        return huaLongElderVoList;
    }

    /**
     * 长者详情
     *
     * @param id the id
     * @return the hua long elder vo
     */
    @Override
    public Object getHuaLongElderId(Integer id) {

        String url = String.format(ELDER_INFO_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), id);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);

        if (response.getBody().getCode() == 0) {
            // 成功
//            huaLongElderVo = JSON.parseObject(JSON.toJSONString(response.getBody().getData()), new TypeReference<HuaLongElderVo>() {});
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }
    }

    /**
     * 获取楼栋房间
     *
     * @return the hua long build info vo
     */
    @Override
    public Object getHuaLongBuildList() {

        String url = String.format(BUILD_LIST_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret());

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);
        HuaLongBuildInfoVo huaLongBuildInfoVo;
        if (response.getBody().getCode() == 0) {
            // 成功
//            huaLongBuildInfoVo = JSON.parseObject(JSON.toJSONString(response.getBody().getData()), new TypeReference<HuaLongBuildInfoVo>() {});
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }

//        return huaLongBuildInfoVo;
    }

    /**
     * 获取房间床位/老人信息
     *
     * @param roomId the room id
     * @return the list hua long room bed vo
     */
    @Override
    public Object getHuaLongRoomBedInfo(Integer roomId) {

        String url = String.format(ROOM_BED_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret());

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap map = new LinkedMultiValueMap();
        map.add("room_id", roomId.toString());

        HttpEntity request = new HttpEntity<>(map, headers); //组装

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.POST, request, HuaLongVo.class);
        HuaLongRoomInfoVo huaLongRoomInfoVo;
        if (response.getBody().getCode() == 0) {
            // 成功
//            huaLongRoomInfoVo = JSON.parseObject(JSON.toJSONString(response.getBody().getData()), new TypeReference<HuaLongRoomInfoVo>() {});
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }

//        return huaLongRoomInfoVo;
    }

    /**
     * 医生查看预约详情
     *
     * @param id 预约id
     * @return the object.
     */
    @Override
    public Object getHuaLongAppointmentInfo(Integer id) {

        String url = String.format(APPOINTMENT_INFO_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), id);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);

        if (response.getBody().getCode() == 0) {
            // 成功
//            huaLongElderVo = JSON.parseObject(JSON.toJSONString(response.getBody().getData()), new TypeReference<HuaLongElderVo>() {});
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }
    }

    /**
     * 医生可查看预约列表
     *
     * @param page 页数 (默认为1)
     * @param size 每页显示条数（默认为10）
     *
     * @return the list hua long appointment vo
     */
    @Override
    public Object getHuaLongAppointmentList(Integer page, Integer size) {

        if (PublicUtil.isEmpty(page)) {
            page = 1;
        }
        if (PublicUtil.isEmpty(size)) {
            size = 10;
        }
        String url = String.format(APPOINTMENT_LIST_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret(), page, size);

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.GET, null, HuaLongVo.class);

        List<HuaLongAppointmentVo> huaLongAppointmentVoList;
        if (response.getBody().getCode() == 0) {
            // 成功
//            JSONObject object = JSONObject.fromObject(response.getBody().getData());
//            huaLongAppointmentVoList = (List<HuaLongAppointmentVo>) object.get("lists");
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }

//        return huaLongAppointmentVoList;
    }

    /**
     * 医生审核预约信息
     *
     * @param huaLongDoctorAuditDto the hua long doctor audit dto
     * @return the object
     */
    @Override
    public Object huaLongDoctorAuditElder(HuaLongDoctorAuditDto huaLongDoctorAuditDto) {

        String url = String.format(DOCTOR_AUDIT_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret());

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        MultiValueMap map = new LinkedMultiValueMap();
        map.add("audit_id", huaLongDoctorAuditDto.getAuditId());
        map.add("doctor_id", huaLongDoctorAuditDto.getDoctorId());
        map.add("type", huaLongDoctorAuditDto.getType());
        map.add("reason", huaLongDoctorAuditDto.getReason());

        FileSystemResource fileSystemResource = new FileSystemResource(huaLongDoctorAuditDto.getSignature());
//        FileSystemResource fileSystemResource = new FileSystemResource("G:\\1.jpg");
        map.add("signature", fileSystemResource);


        HttpEntity request = new HttpEntity<>(map, headers); //组装

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.POST, request, HuaLongVo.class);
        HuaLongRoomInfoVo huaLongRoomInfoVo;
        if (response.getBody().getCode() == 0) {
            // 成功
//            huaLongRoomInfoVo = JSON.parseObject(JSON.toJSONString(response.getBody().getData()), new TypeReference<HuaLongRoomInfoVo>() {});
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }
    }

    /**
     * 医生端（修改密码）
     *
     * @param id         医生id
     * @param password   输入密码
     * @param repassword 确认密码
     * @return the object
     */
    @Override
    public Object doctorModifyPwd(Integer id, String password, String repassword) {

        String url = String.format(DOCTOR_MODIFY_PWD_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret());

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        MultiValueMap map = new LinkedMultiValueMap();
        map.add("id", id);
        map.add("password", password);
        map.add("repassword", repassword);

        HttpEntity request = new HttpEntity<>(map, headers); //组装

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.POST, request, HuaLongVo.class);
        if (response.getBody().getCode() == 0) {
            // 成功
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }
    }

    /**
     * 护士端（修改密码）
     *
     * @param id         医生id
     * @param password   输入密码
     * @param repassword 确认密码
     * @return the object
     */
    @Override
    public Object nurseModifyPwd(Integer id, String password, String repassword) {

        String url = String.format(NURSE_MODIFY_PWD_URL,
                cmnyProperties.getHualong().getHost(),
                cmnyProperties.getHualong().getKey(),
                cmnyProperties.getHualong().getSecret());

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        MultiValueMap map = new LinkedMultiValueMap();
        map.add("id", id);
        map.add("password", password);
        map.add("repassword", repassword);

        HttpEntity request = new HttpEntity<>(map, headers); //组装

        ResponseEntity<HuaLongVo> response = myRestTemplate.exchange(url, HttpMethod.POST, request, HuaLongVo.class);
        if (response.getBody().getCode() == 0) {
            // 成功
            return response.getBody().getData();
        } else {
            // 失败
            throw new BusinessException(response.getBody().getMsg());
        }
    }


    public static void main(String[] args) {


//        String loginUrl = String.format("%sshj/v1/staff/login?key=%s&secret=%s", host, key, secret);
//
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//
//        JSONObject obj = new JSONObject();//放入body中的json参数
//        obj.put("account", "13822222222");
//        obj.put("password", "password");
//        obj.put("role", 3);
//
//        HttpEntity<JSONObject> request = new HttpEntity<>(obj,headers); //组装
//
//        ResponseEntity<String> response = restTemplate.exchange(loginUrl, HttpMethod.POST, request,String.class);

        System.out.println("md5 = " + MD5Util.getMD5String("123456"));
    }
}
